package com.newland.mesdk.demo.fragment;

import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;

import com.newland.mesdk.demo.main.R;
import com.newland.mtype.module.common.light.LightType;
import com.newland.mesdk.demo.interfaceimpl.IndicatorLightInterfaceImpl;
import com.newland.mesdk.demo.main.MainActivity;
import com.newland.mesdk.demo.util.Const.MessageTag;

/**
 * Created by YJF 指示灯模块
 */
public class LightFragment extends Fragment implements OnClickListener {
	private MainActivity mainActivity;
	private IndicatorLightInterfaceImpl indicatorLightInterfaceImpl;
	private Button btnOperateMore, btnPowerOn, btnPowerOff;

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		View view = inflater.inflate(R.layout.fragment_light, null);
		mainActivity = (MainActivity) getActivity();
		initLightView(view);
		return view;
	}

	public void initLightView(View view) {

		btnOperateMore = (Button) view.findViewById(R.id.btn_light_operate_more);
		btnOperateMore.setOnClickListener(this);

		btnPowerOn = (Button) view.findViewById(R.id.btn_light_poweron);
		btnPowerOn.setOnClickListener(this);

		btnPowerOff = (Button) view.findViewById(R.id.btn_light_powerof);
		btnPowerOff.setOnClickListener(this);
	}

	@Override
	public void onClick(View v) {
		if (mainActivity.isDeviceInit()) {
			if (mainActivity.processingisLocked()) {
				mainActivity.showMessage("请先完成或撤销当前操作...", MessageTag.ERROR);
			} else {
				mainActivity.connectDevice();
				indicatorLightInterfaceImpl = new IndicatorLightInterfaceImpl();
				switch (v.getId()) {
				case R.id.btn_light_operate_more:
					new Thread(new Runnable() {

						@Override
						public void run() {
							try {
									mainActivity.processingLock();
									Boolean operateMultLightResult = indicatorLightInterfaceImpl.operateLight(new LightType[] { LightType.RED_LIGHT,
											LightType.BLUE_LIGHT, LightType.GREEN_LIGHT }, 8, 100);
									if (operateMultLightResult) {
										mainActivity.showMessage("对多个指示灯进行闪烁操作：" + "成功" + "\r\n", MessageTag.NORMAL);
										mainActivity.processingUnLock();
									} else {
										mainActivity.showMessage("对多个指示灯进行闪烁操作：" + "失败" + "\r\n", MessageTag.NORMAL);
										mainActivity.processingUnLock();
									}

							} catch (Exception e) {
								mainActivity.showMessage("对多个指示灯进行闪烁操作：" + "异常！" + e + "\r\n", MessageTag.ERROR);
								mainActivity.processingUnLock();
							}

						}
					}).start();

					break;
				case R.id.btn_light_poweron:
					new Thread(new Runnable() {

						@Override
						public void run() {
							try {
								
									mainActivity.processingLock();
									Boolean turnOnLightResult = indicatorLightInterfaceImpl.turnOnLight(new LightType[] { LightType.RED_LIGHT,
											LightType.GREEN_LIGHT, LightType.YELLOW_LIGHT, LightType.BLUE_LIGHT });
									if (turnOnLightResult) {
										mainActivity.showMessage("对蓝、黄、红色指示灯进行常亮操作：" + "成功" + "\r\n", MessageTag.NORMAL);
										mainActivity.processingUnLock();
									} else {
										mainActivity.showMessage("对蓝、黄、红色指示灯进行常亮操作：" + "失败" + "\r\n", MessageTag.NORMAL);
										mainActivity.processingUnLock();
									}

							} catch (Exception e) {
								mainActivity.showMessage("对蓝、黄、红色指示灯进行常亮操作：" + "异常！" + e + "\r\n", MessageTag.ERROR);
								mainActivity.processingUnLock();
							}


						}
					}).start();

					break;
				case R.id.btn_light_powerof:
					new Thread(new Runnable() {

						@Override
						public void run() {
							try {
									mainActivity.processingLock();
									Boolean trunOffLightResult = indicatorLightInterfaceImpl.turnOffLight(new LightType[] { LightType.BLUE_LIGHT,
											LightType.RED_LIGHT, LightType.GREEN_LIGHT, LightType.YELLOW_LIGHT });
									if (trunOffLightResult) {
										mainActivity.showMessage("对指示灯进行熄灭操作：" + "成功" + "\r\n", MessageTag.NORMAL);
										mainActivity.processingUnLock();
									} else {
										mainActivity.showMessage("对指示灯进行熄灭操作：" + "失败" + "\r\n", MessageTag.NORMAL);
										mainActivity.processingUnLock();
									}

							} catch (Exception e) {
								mainActivity.showMessage("对指示灯进行熄灭操作：" + "异常！" + e + "\r\n", MessageTag.ERROR);
								mainActivity.processingUnLock();
							}
						}
					}).start();

					break;

				default:
					break;
				}
			}
		} else {
			mainActivity.showMessage("设备未初始化，请先初始化！" + "\r\n", MessageTag.NORMAL);
		}
	}

	

}
